package com.light.gulimall.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 设置feign的过滤规则
 */
@Configuration
public class FeignConfig implements RequestInterceptor {


    /**
     * Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
     *
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
        //得到原生的请求头数据
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        //给feign的模版的header中放入cookie
        String cookie = request.getHeader("Cookie");
        template.header("Cookie", cookie);

    }
}
